import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:sms/sms.dart';

String message = "";
String sendResult = "";
Dio dio = new Dio();

void main() {
  runApp(MyApp());

  SmsReceiver receiver = new SmsReceiver();
  receiver.onSmsReceived.listen((SmsMessage msg) {
    message = msg.dateSent.toString() + "收到来自 " + msg.address + " 短信, 内容是：" + msg.body;
    var post = dio.get("https://5gmall.inspur.com:9100/ict/mobileSms/add?sms=" + message);
    post.then((value) {
      print(value);
      sendResult = value.toString();
    });
  });
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: '自动读取短信内容'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _message = "";
  String _sendResult = "";
  String _time = "";
  Timer _timer;

  @override
  void initState() {
    super.initState();
    _timer = Timer.periodic(Duration(milliseconds: 5000), (timer) {
      _incrementCounter();
    });
  }

  void _incrementCounter() {
    setState(() {
      _message = message;
      _sendResult = sendResult;
      _time = new DateTime.now().toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              child: new Text("当前时间：$_time"),
              margin: const EdgeInsets.all(5.0),
              padding: const EdgeInsets.all(5.0),
            ),
            new Container(
              child: new Text(
                " ------ 短信内容 ------ ",
                style: TextStyle(color: Colors.red),
              ),
              margin: const EdgeInsets.all(5.0),
              padding: const EdgeInsets.all(5.0),
            ),
            new Container(
              child: new Text(_message),
              alignment: Alignment.topLeft,
              margin: const EdgeInsets.all(5.0),
              padding: const EdgeInsets.all(5.0),
              decoration: BoxDecoration(
                border: new Border.all(color: Colors.red, width: 0.5), // 边色与边宽度
                borderRadius: new BorderRadius.vertical(top: Radius.circular(5), bottom: Radius.circular(5)),
              ),
            ),
            new Container(
              child: new Text(
                " ------ 发送结果 ------ ",
                style: TextStyle(color: Colors.blueAccent),
              ),
              margin: const EdgeInsets.all(5.0),
              padding: const EdgeInsets.all(5.0),
            ),
            new Container(
              child: new Text(_sendResult),
              alignment: Alignment.topLeft,
              margin: const EdgeInsets.all(5.0),
              padding: const EdgeInsets.all(5.0),
              decoration: BoxDecoration(
                border: new Border.all(color: Colors.blueAccent, width: 0.5), // 边色与边宽度
                borderRadius: new BorderRadius.vertical(top: Radius.circular(5), bottom: Radius.circular(5)),
              ),
            ),
            new Container(
              child: new Text('By li.peng02@inspur.com'),
              alignment: Alignment.center,
              margin: const EdgeInsets.fromLTRB(5.0, 50, 5.0, 0.0),
              padding: const EdgeInsets.all(5.0),
            ),
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
